package com.insea.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

@Entity
public class Examen extends BaseObject{

	private Long id;
	private String numExamen;
	private Date dateDebutExamen;
	private Date dateFinExamen;
	private String crExamen;
	private boolean statut;
	private Facture facture;
	private User user;
	private Patient patient;
	private Set<Acte> actes = new HashSet<Acte>();
	



	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "numExamen")
	public String getNumExamen() {
		return this.numExamen;
	}

	public void setNumExamen(String numExamen) {
		this.numExamen = numExamen;
	}


	@Column(name = "crExamen")
	public String getCrExamen() {
		return this.crExamen;
	}

	public void setCrExamen(String crExamen) {
		this.crExamen = crExamen;
	}

	@Column(name = "dateDebutExamen")
	public Date getDateDebutExamen() {
		return dateDebutExamen;
	}

	public void setDateDebutExamen(Date dateDebutExamen) {
		this.dateDebutExamen = dateDebutExamen;
	}

	public Date getDateFinExamen() {
		return dateFinExamen;
	}

	public void setDateFinExamen(Date dateFinExamen) {
		this.dateFinExamen = dateFinExamen;
	}

	@Column(name="statut")
	public boolean isStatut() {
		return statut;
	}

	public void setStatut(boolean statut) {
		this.statut = statut;
	}

	@OneToOne
    @JoinColumn(name="FACTURE_ID")
	public Facture getFacture() {
		return facture;
	}

	public void setFacture(Facture facture) {
		this.facture = facture;
	}

	@ManyToOne
    @JoinColumn(name="USER_ID", nullable=false)
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@ManyToOne
    @JoinColumn(name="PATIENT_ID")
	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	 @ManyToMany(fetch = FetchType.EAGER)
	 @JoinTable(
	            name = "examen_acte",
	            joinColumns = { @JoinColumn(name = "examen_id") },
	            inverseJoinColumns = @JoinColumn(name = "acte_id")
	    )
	public Set<Acte> getActes() {
		return actes;
	}

	public void setActes(Set<Acte> actes) {
		this.actes = actes;
	}
	
	@Transient
	public List<Acte> getListActe(){
		List<Acte> listeActe = new ArrayList<Acte>();
		 if (this.actes != null) {
	            for (Acte acte : actes) {
	                listeActe.add(acte);
	            }
	        }
		return listeActe;
	}
	
	/**
     * Adds a acte for the examen
     *
     * @param role the fully instantiated role
     */
    public void addActe(Acte acte) {
        getActes().add(acte);
    }
    
    public void addActes(List<Acte> actes){
    	getActes().addAll(actes);
    }

	public boolean equals(Object other) {
		if ((this == other))
			return true;
		if ((other == null))
			return false;
		if (!(other instanceof Examen))
			return false;
		Examen castOther = (Examen) other;

		return ((this.getNumExamen() == castOther.getNumExamen()));
						
	}

	public int hashCode() {
		int result = 17;

		result = 37 * result
				+ (getNumExamen() == null ? 0 : this.getNumExamen().hashCode());
		
		return result;
	}

	@Override
	public String toString() {
		return numExamen;
	}
	
}
